package com.example.onlinemusicserver.controller;

import com.example.onlinemusicserver.model.Collect;
import com.example.onlinemusicserver.model.Music;
import com.example.onlinemusicserver.model.User;
import com.example.onlinemusicserver.service.CollectService;
import com.example.onlinemusicserver.tools.Constant;
import com.example.onlinemusicserver.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/collect")
public class CollectController {
    @Autowired
    private CollectService collectService;

    /**
     * 点击收藏的时候, 收藏音乐
     * @param musicId
     * @param request
     * @return
     */
    @RequestMapping("/loveMusic")
    public ResponseBodyMessage<Boolean> AddLoveMusic(@RequestParam String musicId, HttpServletRequest request) {
        int music_Id = Integer.parseInt(musicId);
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(Constant.USER_SESSION_KEY);
        int userId = user.getUserId();

        Collect collect = collectService.findCollectMusic(userId,music_Id);
        if (collect != null) {
            return new ResponseBodyMessage<>(-1, "当前已经收藏了",false);
        }else{
            int ret = collectService.insertLoveMusic(userId,music_Id);
            if(ret == 1) {
                return new ResponseBodyMessage<>(1, "收藏成功!",true);
            }else{
                return new ResponseBodyMessage<>(-1,"收藏失败",false);
            }
        }
    }

    /**
     * 1. 空查询, 查找所有的收藏音乐
     * 2. 模糊查询, 查询包含部分 musicName 的所有收藏音乐
     * @param musicName
     * @param request
     * @return
     */
    @RequestMapping("findLoveMusic")
    public ResponseBodyMessage<HashMap<String,Object>> findLoveMusic(@RequestParam(required = false) String musicName,
                                                          HttpServletRequest request,
                                                          Integer pageIndex) {
        HttpSession session = request.getSession(false);
        HashMap<String,Object> hash = new HashMap<>();
        if(session == null) {
            return new ResponseBodyMessage<>(-1,"当前未登录",null);
        }
        User user = (User) session.getAttribute(Constant.USER_SESSION_KEY);
        int userId = user.getUserId();
        if(pageIndex <= 0) {
            return new ResponseBodyMessage<>(-1,"下标错误",null);
        }
        int pageRange = (pageIndex - 1) * Constant.PAGE_SIZE;
        int totalMusic = collectService.totalMusic(musicName,userId);
        if(totalMusic != 0 && pageIndex > Math.ceil(totalMusic*1.0/Constant.PAGE_SIZE)){
            return new ResponseBodyMessage<>(-1,"下标错误",null);
        }
        List<Music> list = null;
        if(musicName == null || "".equals(musicName)) {
            list = collectService.findLoveMusicByUserId(userId,pageRange,Constant.PAGE_SIZE);
        }else{
            list = collectService.findLoveMusicByNameAndUserId(musicName,userId,pageRange,Constant.PAGE_SIZE);
        }
        hash.put("list",list);
        hash.put("totalMusic",totalMusic);
        return new ResponseBodyMessage<>(1,"查询成功!",hash);
    }

    /**
     * 删除收藏的音乐
     * @param musicId
     * @param request
     * @return
     */
    @RequestMapping("/deleteLoveMusic")
    public ResponseBodyMessage<Boolean> deleteLoveMusic(@RequestParam String musicId,HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if(session == null) {
            return new ResponseBodyMessage<>(-1,"当前未登录",false);
        }
        User user = (User) session.getAttribute(Constant.USER_SESSION_KEY);
        int userId = user.getUserId();
        int ret =  collectService.deleteLoveMusic(userId,Integer.parseInt(musicId));
        if(ret == 1) {
            return new ResponseBodyMessage<>(1,"取消收藏成功!",true);
        }else{
            return new ResponseBodyMessage<>(-1,"取消收藏失败!",false);
        }
    }
}
